import { AfterViewInit, Component, ElementRef, EventEmitter, Injector, Output, ViewChild, Input, OnInit } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { ActivatedRoute } from '@angular/router';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ProfileServiceProxy,  TaskServiceProxy, WorkflowServiceProxy, UserServiceProxy, SupplierQuotationServiceProxy, OutsourcingWorkflowServiceProxy,   ExecutionPointerDto } from '@shared/service-proxies/service-proxies';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { TaskService } from '../task/Task.service';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';
import { FileDownloadService } from '@shared/utils/file-download.service';

@Component({
    selector: 'OrdinaryCrucialDetail',
    templateUrl: './OrdinaryCrucialDetail.component.html',
    styleUrls: ['./OrdinaryCrucialDetail.component.css']
})
export class OrdinaryCrucialDetailComponent extends AppComponentBase implements OnInit {
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('taskdataTable') taskdataTable: Table;
    @ViewChild('dataTable') dataTable: Table;

    @Input() workflowid: any;
    @Output() out = new EventEmitter();

    active = false;
    saving = false;
    Outsourcings;
    tabPanels = [];
    supplierUnits = [];
    workflow;
    applyUseranme: string;
    detailsprimengTableHelper: PrimengTableHelper;

    ngOnInit(): void {

        console.log(this.workflowid);
        this.initForm();
    }
    // tslint:disable-next-line: use-life-cycle-interface
    ngAfterViewInit(): void {
        console.log(this.workflowid);
    }

    constructor(injector: Injector,
        private route: ActivatedRoute,
        private _taskServiceProxy: TaskServiceProxy,
        private _filedownloadService: FileDownloadService,
        private _outSourcingworkflowServiceProxy: OutsourcingWorkflowServiceProxy,
        private _supplierServiceProxy: SupplierQuotationServiceProxy,
        private _taskService: TaskService,
        private _profileService: ProfileServiceProxy,
        private _userServiceProxy: UserServiceProxy,
        private _workflowService: WorkflowServiceProxy) {
        super(injector);
        this.detailsprimengTableHelper = new PrimengTableHelper();
    }

    initForm(): void {
        console.log(this.workflowid);
        this._outSourcingworkflowServiceProxy.getOutsourcingWorkFlow(this.workflowid).subscribe(dto => {
            this.workflow = dto.workflowDto;
            console.log(dto);
            this.supplierUnits = this.workflow.supplierUnit.split(',');
            _.forEach(this.supplierUnits, viewItem => {
                let _a = { title: viewItem };
                this.tabPanels.push(_a);
            });
            this.active = true;
        });
    }

    getOutsourings(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);

            return;
        }
        this.primengTableHelper.showLoadingIndicator();

        this._outSourcingworkflowServiceProxy.getOutsourcingByWorkflowId(this.workflowid,
            '',
            5,
            this.primengTableHelper.getSkipCount(this.paginator, event))
            .pipe(
                finalize(() => this.primengTableHelper.hideLoadingIndicator())
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });

    }


    getAuditName(item: ExecutionPointerDto) {
        return item.extensionAttributes.Prompt;
    }
    exportToexcel(): void {

        this._supplierServiceProxy.supplierQuotationForExcel(this.workflowid).subscribe(dto => {
            this._filedownloadService.downloadTempFile(dto);
        });
    }
}

